package imp

import (
	sqlc "bgs/db/sqlc"
	storeGrpc "bgs/grpc/gen/service/store"
	"bgs/grpc/gen/shared/message"
	"bgs/grpc/imp/shared"
	productService "bgs/service/product"
	"bgs/util"
	"context"
)

// ListStoreProductGroups  获取门店的商品分组列表
func (s *StoreAPIService) ListStoreProductGroups(ctx context.Context, req *storeGrpc.ListStoreProductGroupsRequest) (res *storeGrpc.ListStoreProductGroupsResponse, err error) {
	slog := s.NewContextLogger(ctx)
	slog.Infof("store-api/product->ListStoreProductGroups:%v", req)
	res = &storeGrpc.ListStoreProductGroupsResponse{
		OpRet: &message.OperationResult{
			Success: true,
			Code:    0,
		},
	}
	q := s.dao.Q
	userID := req.UserId
	storeID := req.StoreId

	distributorPtr, err := s.getDistributorByUserIDAndStoreID(ctx, q, userID, storeID)
	if err != nil {
		return
	}
	total, ptrs, err := s.productService.ListNormalProductGroupPtrsOfDistributor(ctx, q, distributorPtr, &util.Pager{
		Limit:  req.Limit,
		Offset: req.Offset,
	})
	if err != nil {
		return
	}
	res.Total = total
	res.ProductGroups = shared.MapProductGroupEntityToPb(ptrs, shared.TransformProductGroupEntityToPb)
	return
}

// ListBrandProductWithDistributeInfos  列出品牌供货商品
func (s *StoreAPIService) ListStoreProductWithDistributeInfos(ctx context.Context, req *storeGrpc.ListStoreProductWithDistributeInfosRequest) (res *storeGrpc.ListStoreProductWithDistributeInfosResponse, err error) {
	slog := s.NewContextLogger(ctx)
	slog.Infof("store-api/product->ListStoreProductWithDistributeInfos:%v", req)
	res = &storeGrpc.ListStoreProductWithDistributeInfosResponse{
		OpRet: &message.OperationResult{
			Success: true,
			Code:    0,
		},
	}
	q := s.dao.Q
	ts := sqlc.PBTimestampToGoTime(req.Ts)
	userID := req.UserId
	storeID := req.StoreId
	productGroupID := req.ProductGroupId
	keyword := req.Keyword
	distributorPtr, err := s.getDistributorByUserIDAndStoreID(ctx, q, userID, storeID)
	if err != nil {
		return
	}
	total, ptrs, err := s.productService.ListNormalProductWithDistributeInfoPtrsOfDistributor(ctx, q, distributorPtr,
		&util.Pager{
			Ts:     ts,
			Limit:  req.Limit,
			Offset: req.Offset,
		}, &productService.ListNormalProductWithDistributeInfoPtrsParam{

			ProductGroupID: productGroupID,
			Keyword:        keyword,
		})
	if err != nil {
		return
	}

	productIDs := util.MapToInt64(util.ToGenericSlice(ptrs), func(v interface{}) int64 {
		val := v.(*sqlc.ListNormalProductWithDistributeInfosByBrandIDRow)
		return val.ID
	})

	productAttributeKeys, err := q.ListNormalProductAttributeKeysByProductIDs(ctx, productIDs)
	if err != nil {
		return
	}

	productAttributeValues, err := q.ListNormalProductAttributeValuesByProductIDs(ctx, productIDs)
	if err != nil {
		return
	}

	productSpecs, err := q.ListNormalProductSpecsByProductIDs(ctx, productIDs)
	if err != nil {
		return
	}

	res.Total = total
	res.ProductWithDistributeInfos = shared.MapListNormalProductWithDistributeInfosByBrandIDRowToProductWithDistributeInfoPb(ptrs,
		shared.TransformListNormalProductWithDistributeInfosByBrandIDRowToProductWithDistributeInfoPb(productAttributeKeys, productAttributeValues, productSpecs))
	return
}

// ListStoreProductWithDistributeInfosByProductIDs  按品牌和商品id列出供货商品 
func (s *StoreAPIService) ListStoreProductWithDistributeInfosByProductIDs(ctx context.Context, req *storeGrpc.ListStoreProductWithDistributeInfosByProductIDsRequest) (res *storeGrpc.ListStoreProductWithDistributeInfosByProductIDsResponse, err error) {
	slog := s.NewContextLogger(ctx)
	slog.Infof("store-api/product->ListStoreProductWithDistributeInfosByProductIDs:%v", req)
	res = &storeGrpc.ListStoreProductWithDistributeInfosByProductIDsResponse{
		OpRet: &message.OperationResult{
			Success: true,
			Code:    0,
		},
	}
	q := s.dao.Q
	
	userID := req.UserId
	storeID := req.StoreId
	productIDs := req.ProductIds

	distributorPtr, err := s.getDistributorByUserIDAndStoreID(ctx, q, userID, storeID)
	if err != nil {
		return
	}
	brandID := distributorPtr.BrandID
	ptrs, err := s.productService.ListNormalProductWithDistributeInfoPtrsOfBrandByProductIDs(ctx, q, brandID, productIDs)
	if err != nil {
		return
	}

	productAttributeKeys, err := q.ListNormalProductAttributeKeysByProductIDs(ctx, productIDs)
	if err != nil {
		return
	}

	productAttributeValues, err := q.ListNormalProductAttributeValuesByProductIDs(ctx, productIDs)
	if err != nil {
		return
	}

	productSpecs, err := q.ListNormalProductSpecsByProductIDs(ctx, productIDs)
	if err != nil {
		return
	}

	res.ProductWithDistributeInfos = shared.MapListNormalProductWithDistributeInfosByBrandIDAndProductIDsRowToProductWithDistributeInfoPb(ptrs,
		shared.TransformListNormalProductWithDistributeInfosByBrandIDAndProductIDsRowToProductWithDistributeInfoPb(productAttributeKeys, productAttributeValues, productSpecs))
	return
}